package com.wenx.activeMqCamel;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.camel.component.ActiveMQComponent;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Message;
import org.apache.camel.Processor;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.RouteDefinition;

import javax.jms.ConnectionFactory;

import static org.apache.camel.builder.Builder.constant;

/**
 * 通过虚拟主题持久化订阅
 */
public class activeMQConsumer5 {
    private static String user = "admin";
    private static String password = "admin";
    //填写AMQ的IP
    private static String url = "tcp://10.10.3.186:61616";
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        //创建AMQ的连接工厂
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
        ActiveMQComponent activeMQComponent = new ActiveMQComponent();
        activeMQComponent.setConnectionFactory(connectionFactory);
        //使用连接池连接
        activeMQComponent.setUsePooledConnection(true);
        //事务
        activeMQComponent.setTransacted(true);
        //客户端确认
        activeMQComponent.setAcknowledgementMode(3);
//        //clientid
//        activeMQComponent.setClientId("bbb");
//        //topic只有在持久订阅（durablesubscription）下是持久化的
//        activeMQComponent.setDurableSubscriptionName("bbb");
        context.addComponent("activemq", activeMQComponent);
        System.out.println(url + " " + user + " " + password);
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        context.addRouteDefinition(initRoute());
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程`
        context.stop(); // 最后停止CamelContext
    }
    public static RouteDefinition initRoute(){

        RouteDefinition routeDefinition = new RouteDefinition();
        routeDefinition.from("activemq:queue:Consumer.A.VirtualTopic.test1")
                .doTry()
                .setHeader(Exchange.HTTP_METHOD, constant("POST"))
                //.setHeader(Exchange.CONTENT_TYPE,constant("application/json"))
                .to("http://localhost:8088/rest/api/restful/stringData")
                    .process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            Message in = exchange.getIn();
                            System.out.println("成功");
                        }
                    })
                .doCatch(Exception.class)
                    .process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            System.out.println("异常");
                            System.out.println(exchange.getProperty(Exchange.EXCEPTION_CAUGHT));
                            System.out.println(exchange.getProperty(Exchange.ERRORHANDLER_HANDLED));
                        }
                    })
                .endDoTry();
        return routeDefinition;
    }
}
